package com.chenjj.etl.datatrans.test;

import com.chenjj.etl.datatrans.converter.DataConverter;
import com.chenjj.etl.datatrans.hdfs.HdfsClientUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.security.PrivilegedAction;


public class GzFileProcessTester {

    private static Logger logger = LoggerFactory.getLogger(GzFileProcessTester.class);

    /**
     * kerberos认证模式下执行数据文件转换操作
     */
    @Test
    public void  test1() throws IOException {
        final String gzFilePath = "/user/clientA/saspoc/source/MA_SUBJ_LKP.20150331.000000.0000.dat.gz";
        final String flgFilePath = "/user/clientA/saspoc/source/MA_SUBJ_LKP.20150331.000000.0000.flg";
        final String outputFilePath = "/user/clientA/saspoc/output/MA_SUBJ_LKP/MA_SUBJ_LKP.20150331.000000.0000.dat";
        final String characterset = "GBK";
        final String krb5File = "D:/Temp/krb5.conf";
        final String keytabUser = "clientA@DC.COM";
        final String keytabFilePath = "D:/Temp/clientA.keytab";
        System.setProperty("sun.security.krb5.debug", "false");
        System.setProperty("java.security.krb5.conf", krb5File);
        Configuration conf = new Configuration();
        conf.set("hadoop.security.authentication","kerberos");
        UserGroupInformation.setConfiguration(conf);
        UserGroupInformation.loginUserFromKeytab(keytabUser,keytabFilePath);
        UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(keytabUser,keytabFilePath);
        ugi.doAs(new PrivilegedAction<Object>() {
            public Object run() {
//               如果hdfs-site.xml、yarn-site.xml、core-site.xml在classpath下，则不需要显示的addResource
//                Configuration conf = new Configuration();
//                conf.addResource("hdfs-site.xml");
//                conf.addResource("yarn-site.xml");
//                conf.addResource("core-site.xml");
//                HdfsClientUtil.setInstance(conf);
                DataConverter.convertGzFile(gzFilePath,flgFilePath,outputFilePath,characterset);
                return null;
            }
        });
    }

    @Test
    public void test2(){
        String s = "a.ejkmdm,a.sxrq,a.ejkmmc,a.yjkmdm,a.yjkmmc,a.txdm,a.kmxz,a.jxbz,a.fxcbbz,a.sxzcfxfzbz,a.fxzcbz,a.gkcpdm,a.fxzccpbh,a.jjzbfpxs,a.gctxdm,a.gcgkcpbh,a.yxbz,a.cjrq,a.cjyh,a.gxrq,a.gxyh\n" +
                "193140,20140630,代理发行国家债券,1931,代理发行国家债券_月批修正名称,10,7,,0,0,0,,,0,10,,1,20091008,郭瑜,,";
        System.out.println(s.getBytes().length);
    }
}
